define(['../../../carrys/jsonList','../../../../../js/lib/other/sweet-alert'],function(jsonData) {
    return ['$scope','$location', '$interval','$timeout','$routeParams','bizProxy', function($scope,$location,$interval,$timeout,$routeParams,bizProxy) {
		$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','mine'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    	$scope.$emit('flag-tag','25');//显示底部文字
    	
    	//默认值
    	$scope.jsonList = jsonData;
    	$scope.creditorType = "";$scope.applicantType = "";$scope.idType = "";
    	
    	if($routeParams.flag != "" && $routeParams.flag != null){
    		queryLoanApplyOutline($routeParams.flag);
    	}
    	
    	//点击确认修改
    	$scope.confirmEdit = function(){
    		if(! $scope.inviteCode){
    			$scope.$emit("promptAlertMsg","律师推荐码不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}
    		else if(! $scope.applicantType){
    			$scope.$emit("promptAlertMsg","申请人类别不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.applyName){
    			$scope.$emit("promptAlertMsg","申请人不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.idType){
    			$scope.$emit("promptAlertMsg","证件类别不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.documentsNo){
    			$scope.$emit("promptAlertMsg","证件号码不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.loanAmount){
    			$scope.$emit("promptAlertMsg","贷款金额不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else{
    			if(!_YHC_util.IdentityCodeValid($scope.documentsNo) && $scope.idType === '1'){
    				$scope.$emit("promptAlertMsg","无效身份证号");
        			$scope.$emit("showCollect",true);
        			$timeout(function(){
        				$scope.$emit("showCollect",false);
        			},transData.promptTime);
                    return;
    			}else{
    				submitApplyFor();
    			}
    		}
    	}
    	
    	//点击撤销申请
    	$scope.cancleApply = function(){
    		var sweetData = {
					title : "",
					text : "确认要撤销申请吗？",
					type : "",
					showCancelButton : true,
					closeOnConfirm : false
				};
    		FIREFLY(sweetData,function(){
				$timeout(function(){
					cancelLoanApply();
				},0);
			},function(){return;});
    	}
    	
    	
    	var imageProdUrl = "";
    	document.querySelector('#loan_fail .main .img_show').style.backgroundImage = 'url(http://wx.winhc.cn/showImageById.ajax?imageName='+$routeParams.flag+'.jpg)';
    	$scope.isSlide_up = false;
		//点击上传图片
		$scope.upLoadImage = function(){
			$scope.isSlide_up = true;
		}
		
		//点击打开相机
		$scope.openCamera = function(){
			$scope.isSlide_up = false;
			invokePlugin(Camera.PictureSourceType.CAMERA);
		}
		
		//点击打开相册
		$scope.openPhoto = function(){
			$scope.isSlide_up = false;
			invokePlugin(Camera.PictureSourceType.PHOTOLIBRARY);
		}
		
		//点击取消
		$scope.cancle = function(){
			$scope.isSlide_up = false;
		}
		
		
		//调用插件
		 function invokePlugin(typeObj){
			 var option = {};
//				本地：PHOTOLIBRARY  相机：CAMERA
				option.sourceType = typeObj;
				　//Camera.getPicture(type)->根据选择的“选取图片”的方式进行选取
	            navigator.camera.getPicture(
	　　　　　　　　　 //返回一个imageURI，记录了照片的路径
	                function (imageURI) {
	                	imageProdUrl = imageURI;
	                	document.querySelector('#loan_fail .main .img_show').style.backgroundImage = 'url('+imageURI+')';
	                },
	                function (err) {
	                },option);
		 }
		 
		 //上传图片
		 function upLoadPic(applyId){
//				$scope.$emit("loadingChange",true);
				//新建文件上传选项，并设置文件key，name，type
				var options = new FileUploadOptions();
				options.fileKey="file";
				options.fileName=imageProdUrl.substr(imageProdUrl.lastIndexOf('/')+1);
				options.mimeType="image/jpeg";
				//用params保存其他参数，例如昵称，年龄之类
				var params = {};
				params['name'] = "图片名称";
				params['loanApplyId'] = applyId;
				//把params添加到options的params中
				options.params = params;
				//新建FileTransfer对象
				var ft = new FileTransfer();
				//上传文件
				ft.upload(
					imageProdUrl,
				    encodeURI('http://wx.winhc.cn/uploadImage.ajax'),//把图片及其他参数发送到这个URL，相当于一个请求，在后台接收图片及其他参数然后处理
				    uploadSuccess,
				    uploadError,
				    options);
				//upload成功的话
				function uploadSuccess(r) {
				    if(r.response == "00"){
				　　　　 //返回前一页面
//				        $navHistory.back();
				    	$timeout(function(){
			    			$scope.$emit("loadingChange",false);
				    		$scope.$emit("goNext","/sucLoanApply");
				    	},0);
				    }else{
				    	$timeout(function(){
					    	$scope.$emit("loadingChange",false);
				    	},0);
				    }
				} 
				//upload失败的话
				function uploadError(error) {
				    $scope.$emit("loadingChange",false);
					$scope.$emit("promptAlertMsg","图片上传失败");
					$scope.$emit("showCollect",true);
					$timeout(function(){
						$scope.$emit("showCollect",false);
					},transData.promptTime);
				}
		 }
    	
    	//提交申请到代理
    	function submitApplyFor(){
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
//        	bizInfo.caseName = $scope.caseName;
        	bizInfo.loanApplyId = $routeParams.flag;
        	bizInfo.inviteCode = $scope.inviteCode;
        	bizInfo.applyUserType = $scope.applicantType;
        	bizInfo.applyUserName = $scope.applyName;
        	bizInfo.idType = $scope.idType;
        	bizInfo.idNo = $scope.documentsNo;
        	bizInfo.loanAmount = $scope.loanAmount;
        	bizInfo.debtType = ($scope.creditorType == "" || $scope.creditorType == null)?"":$scope.creditorType;
        	bizInfo.caseNo = $scope.caseNo;
        	bizInfo.caseAmount = $scope.caseQuan;
        	
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("changeLoanApply",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
		    			$timeout(function(){
		    				$scope.$emit("loadingChange",false);
							if(imageProdUrl == ""){
								$scope.$emit("goNext","/sucLoanApply");
							}else{
								upLoadPic($routeParams.flag);
							}
						},0);
		    		},function(data){
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit('loadingChange', false);
		    				$scope.$emit("promptAlertMsg","连接超时");
		    				$scope.$emit("showCollect",true);
		    				$timeout(function(){
		    					$scope.$emit("showCollect",false);
		    				},transData.promptTime);
		    			}
		    		});
    	}
    	
    	
    	//调用代理接口撤销贷款申请
    	function cancelLoanApply(){
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	bizInfo.applyId = $routeParams.flag;
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("cancelLoanApply",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
		    			$timeout(function(){
							$scope.$emit("loadingChange",false);
							history.back();
						},0);
		    		},function(data){
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit('loadingChange', false);
		    				$scope.$emit("promptAlertMsg","连接超时");
		    				$scope.$emit("showCollect",true);
		    				$timeout(function(){
		    					$scope.$emit("showCollect",false);
		    				},transData.promptTime);
		    			}
		    		});
    	}
    	
    	//调用代理接口获取数据
    	function queryLoanApplyOutline(applyId){
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	bizInfo.applyId = applyId;
    		$scope.$emit("loadingChangeNav",true);
    		bizProxy.queryProxyBizData("queryLoanApplyOutline",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
		    			if(tempData.body != "" && tempData.body != null && tempData.body != "[]"){
							$timeout(function(){
								$scope.$emit("loadingChangeNav",false);
								var loanData = JSON.parse(tempData.body);
								$scope.reason = loanData.reason;
								$scope.inviteCode = loanData.inviteCode;
						    	$scope.applicantType = loanData.applyUserType;
						    	$scope.applyName = loanData.applyUserName;
						    	$scope.idType = loanData.idType;
						    	$scope.documentsNo = loanData.idNo;
						    	$scope.loanAmount = loanData.loanAmount;
						    	$scope.creditorType = loanData.debtType;
						    	$scope.caseNo = loanData.caseNo;
						    	$scope.caseQuan = loanData.caseAmount;
		//				    	$scope.caseName = loanData.caseTopic;
							},0);
						}else{
							$scope.$emit("loadingChangeNav",false);
							$scope.$emit("promptAlertMsg","未查找到相关数据");
							$scope.$emit("showCollect",true);
							$timeout(function(){
								$scope.$emit("showCollect",false);
								history.back();
							},transData.promptTime);
						}
		    		},function(data){
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChangeNav",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
									history.back();
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit('loadingChangeNav', false);
		    				$scope.$emit("promptAlertMsg","连接超时");
		    				$scope.$emit("showCollect",true);
		    				$timeout(function(){
		    					$scope.$emit("showCollect",false);
		    					history.back();
		    				},transData.promptTime);
		    			}
		    		});
    	}
        $scope.$apply();
    }];
});